<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <!-- 搜索添加区 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            v-model="queryInfo.query"
            @clear="getOrderList"
          >
            <template #append>
              <el-button
                icon="el-icon-search"
                @click="getOrderList"
              ></el-button>
            </template>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="orderList" border style="width: 100%">
        <el-table-column type="index" width="50" />
        <el-table-column prop="order_number" label="订单编号" />
        <el-table-column prop="order_price" label="订单价格" width="100" />
        <el-table-column prop="pay_status" label="是否付款" width="100">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.order_pay == '1'"
              >已付款</el-tag
            >
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="100" />
        <el-table-column prop="create_time" label="下单时间" width="200" />
        <el-table-column prop="操作" label="操作" width="130px">
          <template #default="scope">
            <!-- 修改地址按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="infoEdit(scope.row)"
            ></el-button>
            <!-- 物流按钮 -->
            <el-button
              type="success"
              icon="el-icon-location"
              size="mini"
              @click="addressProgress(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>

      <!-- 修改地址弹出框 -->
      <el-dialog
        :visible.sync="addressVisible"
        title="修改地址"
        @close="addressDialogClosed"
        width="50%"
      >
        <!-- 内容主体区 -->
        <el-form
          ref="addressFormRef"
          label-width="100px"
          :model="addressForm"
          :rules="addressFormRulse"
        >
          <el-form-item label="省市区/县" prop="address1">
            <el-cascader
              :options="cityData"
              v-model="addressForm.address1"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="addressForm.address2"></el-input>
          </el-form-item>
        </el-form>
        <!-- 底部区域 -->
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="addressVisible = false">取消</el-button>
            <el-button type="primary" @click="addressVisible = false"
              >确定</el-button
            >
          </span>
        </template>
      </el-dialog>
      <!-- 修改信息弹出框 -->
      <el-dialog
        :visible.sync="infoVisible"
        title="修改信息"
        @close="infoDialogClosed"
        width="50%"
      >
        <!-- 内容主体区 -->
        <el-form ref="infoFormRef" label-width="100px" :model="infoForm">
          <el-form-item label="是否发货">
            <el-radio-group v-model="infoForm.is_send">
              <el-radio :label="0">未发货</el-radio>
              <el-radio :label="1">已发货</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否支付">
            <el-radio-group v-model="infoForm.order_pay">
              <el-radio :label="0">未支付</el-radio>
              <el-radio :label="1">支付宝</el-radio>
              <el-radio :label="2">微信</el-radio>
              <el-radio :label="3">银行卡</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="支付状态">
            <el-radio-group v-model="infoForm.pay_status">
              <el-radio :label="0">未支付</el-radio>
              <el-radio :label="1">已支付</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <!-- 底部区域 -->
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="infoVisible = false">取消</el-button>
            <el-button type="primary" @click="infotrue">确定</el-button>
          </span>
        </template>
      </el-dialog>
      <!-- 物流进度弹出框 -->
      <el-dialog
        :visible.sync="addressProgressVisible"
        title="物流进度"
        @close="addressProgressDialogClosed"
        width="50%"
      >
        <!-- activity是数据源，timestamp绑定时间，  -->
        <!-- <el-timeline>
          <el-timeline-item
            v-for="(activity, index) in activities"
            :key="index"
            :timestamp="activity.timestamp"
          >
            {{ activity.content }}
          </el-timeline-item>
        </el-timeline> -->
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import cityData from "../order/citydata";
export default {
  name: "Order",
  data() {
    return {
      infoForm: {
        order_id: "",
        is_send: 0,
        order_pay: 0,
        pay_status: 0,
        order_price: "",
      },
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      // 订单参数
      orderList: [],
      // 订单总数
      total: 0,
      addressVisible: false,
      infoVisible: false,
      addressForm: {
        address1: [],
        address2: "",
      },
      addressFormRulse: {
        address1: [{ required: true, message: "请选择地区", trigger: "blur" }],
        address2: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
      },
      cityData,
      addressProgressVisible: false,
    };
  },
  created() {
    this.getOrderList();
  },
  methods: {
    // 处理时间
    // padStart(2,'0')表示没有2位用0补全
    dataFormat(time) {
      // 这里要乘以1000因为时间戳是用毫秒来计算的
      time = time * 1000;
      const dt = new Date(time);
      // console.log(dt);
      const y = dt.getFullYear();
      // console.log(y);

      const m = (dt.getMonth() + 1 + "").padStart(2, "0");
      // console.log(m);
      const d = (dt.getDate() + "").padStart(2, "0");
      const hh = (dt.getHours() + "").padStart(2, "0");
      const mm = (dt.getMinutes() + "").padStart(2, "0");
      const ss = (dt.getSeconds() + "").padStart(2, "0");
      return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
    },
    async getOrderList() {
      const { data: res } = await this.$http.get("/orders", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200)
        return this.$message.error("获取订单参数失败");
      this.orderList = res.data.goods;
      this.total = res.data.total;
      this.orderList = this.orderList.map((item) => {
        item.create_time = this.dataFormat(item.create_time);
        return item;
      });
      console.log(this.orderList);
    },
    // 监听页码内容大小改变
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize;
      this.getOrderList();
    },
    // 监听页面改变
    handleCurrentChange(newnum) {
      this.queryInfo.pagenum = newnum;
      this.getOrderList();
    },
    // 点击修改地址
    editAddress() {
      this.addressVisible = true;
    },
    //  当修改按钮框关闭时，清空表单
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields();
    },
    async addressProgress() {
      // const { data: res } = await this.$http.get(`/kuaidi/804909574412544580`);
      // console.log(res);
      this.addressProgressVisible = true;
    },
    addressProgressDialogClosed() {
      this.addressProgressVisible = false;
    },
    // 关闭修改商品信息
    infoDialogClosed() {},
    // 点击修改商品信息
    async infoEdit(row) {
      // console.log(row);
      if (row.is_send === "否") {
        this.infoForm.is_send = 0;
      } else {
        this.infoForm.is_send = 1;
      }
      this.infoForm.order_id = row.order_id;
      this.infoForm.order_pay = row.order_pay * 1;
      this.infoForm.pay_status = row.pay_status * 1;
      this.infoForm.order_price = row.order_price;

      this.infoVisible = true;
    },
    async infotrue() {
      console.log(this.infoForm);
      // const { is_send, order_pay, pay_status, order_id, order_price } =
      //   this.infoForm;
      // console.log(pay_status);
      console.log(this.infoForm.order_pay);
      const { data: res } = await this.$http.put(
        `/orders/${this.infoForm.order_id}`,
        {
          is_send: this.infoForm.is_send + "",
          order_price: this.infoForm.order_price + "",
          order_pay: this.infoForm.order_pay + "",
          pay_status: this.infoForm.pay_status + "",
        }
      );
      console.log(res);
      if (res.meta.status !== 201) return this.$message.error("更新订单失败");
      this.infoVisible = false;
      this.getOrderList();
    },
  },
};
</script>

<style lang='less' scoped>
</style>